let express = require('express');
let router = express.Router();

const punchcardService = require('../service/punchcardService');

// 引入jwt验证方法的中间件
const checkJWTMiddleWare = require('../middleWare/checkJWTMiddleWare');
router.use(checkJWTMiddleWare);

/*
 * 用户签到
 */
router.get('/signIn', async function(req, res) {
    let uid = req.jwtResult.data.uid;
    let result = await punchcardService.signIn(uid);
    res.status(result.success ? 200 : 400).json(result);
});

/*
 * 查询当天对应用户是否签到了
 */
router.get('/checkSigned', async function(req, res) {
    let uid = req.jwtResult.data.uid;
    let result = await punchcardService.checkSigned(uid);
    res.status(result.success ? 200 : 400).json(result);
});

/*
 * 查询当月已签到日期
 */
router.get('/getSignedDays', async function(req, res) {
    let uid = req.jwtResult.data.uid;
    let result = await punchcardService.getSignedDays(uid);
    res.status(result.success ? 200 : 400).json(result);
});

/*
 * 分页获取所有每日打卡记录
 */
router.get('/getPunchcard', async function(req, res) {
    let uid = req.jwtResult.data.uid;
    let page = parseInt(req.query.page || 1);
    let result = await punchcardService.getPunchcard(uid, page);
    res.status(result.success ? 200 : 400).json(result);
});

module.exports = router;